package com.liws.helloUpload.service.impl;

import com.liws.helloUpload.mapper.PictureMapper;
import com.liws.helloUpload.model.PicUploadResult;
import com.liws.helloUpload.model.Picture;
import com.liws.helloUpload.service.PictureService;
import lombok.extern.slf4j.Slf4j;
import org.junit.internal.runners.statements.Fail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.sql.rowset.serial.SerialBlob;
import java.io.File;
import java.sql.Blob;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: liweisheng
 * @Dcscription:
 * @Date: 2022/1/5 15:41
 */
@Slf4j
@Service
public class PictureServiceImpl implements PictureService {

    public static final int fail=0;
    public static final int success=1;

    @Autowired
    PictureMapper pictureMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int savePicture(MultipartFile multipartFile, HttpServletRequest request) {

        if (multipartFile.isEmpty()) {
            //返回选择文件提示
            return fail;
        }

        String id = String.valueOf(UUID.randomUUID());
        String name = multipartFile.getOriginalFilename();
        Blob filename = null;
        try {
            filename = new SerialBlob(multipartFile.getBytes()); //将MultipartFile转 Blob
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date created = new Date();
        Picture picture = new Picture();
        picture.setId(id);
        picture.setName(name);
        picture.setFilname(filename);
        picture.setCreated(created);

        int result = pictureMapper.savePicture(picture);

        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveManyPictures(MultipartFile[] uploadFile, HttpServletRequest request) {

        List<Picture> list=new ArrayList<>();
        for (MultipartFile multipartFile : uploadFile) {
            if (multipartFile.isEmpty()) {
                //返回选择文件提示
                return fail;
            }else {
                Picture picture = new Picture();

                String id = String.valueOf(UUID.randomUUID());
                String name = multipartFile.getOriginalFilename();
                Date created = new Date();

                Blob filename = null;
                try {
                   filename = new SerialBlob(multipartFile.getBytes()); //将MultipartFile转 Blob

                    picture.setId(id);
                    picture.setName(name);
                    picture.setFilname(filename);
                    picture.setCreated(created);
                    pictureMapper.savePicture(picture);
                } catch (Exception e) {
                    e.printStackTrace();
                }



                list.add(picture);
            }
        }


        //int result = pictureMapper.saveManyPictures(list);

        return success;
    }
}
